package com.briup.smartlabs.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.common.utils.CrcModbusCheckUtils;
import com.briup.smartlabs.common.utils.MessageConvertUtils;
import com.briup.smartlabs.common.utils.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(tags = "设备控制类服务")
@RestController
public class DeviceControlController {
	@Autowired(required = false)
	private OutputStream spOut;
	
	private String prefix = "efef00";
	private String shortAdres = "ffff";
	private String suffix = "ff";
	
	@ApiOperation(value = "控制rgb灯光显示")
	@GetMapping("/light/rgbLight")
	public Response<String> rgbLight(
			@RequestParam(defaultValue = "0")int red,
			@RequestParam(defaultValue = "0")int green,
			@RequestParam(defaultValue = "0")int blue)throws IOException{
		String rgbAdres = "142303";
		String redHex = Integer.toHexString(red);
		String greenHex = Integer.toHexString(green);
		String blueHex = Integer.toHexString(blue);
		redHex = redHex.length()==1?"0"+redHex:redHex;
		greenHex = greenHex.length()==1?"0"+greenHex:greenHex;
		blueHex = blueHex.length()==1?"0"+blueHex:blueHex;
		rgbAdres = rgbAdres+redHex+greenHex+blueHex;
		sendProtocol(rgbAdres);
		return Response.ok("成功！");
	}
	
	
	@ApiOperation(value = "控制高亮led的显示")
	@ApiImplicitParam(name = "val",value = "灯光亮度(0-1000)")
	@GetMapping("/light/hightLed")
	public Response<String> hightLed(
			@RequestParam(defaultValue = "0")int val)throws IOException{
		String hightLed = "132302";
		String hight = Integer.toHexString(val); //将用户传递的亮度值转换成16进制字符串
		System.out.println("用户传递的亮度转换成的16进制字符串："+hight);
		for(int i = hight.length();i<4;i++) {
			hight = "0"+hight;
		}
		System.out.println("亮度值转换成连个字节，四字符："+hight);
		sendProtocol(hightLed+hight);
		return Response.ok("成功！");
	}
	
	
	
	@ApiOperation(value = "控制三色led灯的显示")
	@ApiImplicitParam(name = "val",value = "颜色值",
					 allowableValues = "0,1,2,3,4,5,6,7",
					 defaultValue = "0")	
	@GetMapping("/light/threeLed")
	public Response<String> threeLed(Integer val) throws Exception{
		if(val==null) {
			val = 0;
		}
		String ledAdres = "112301"+"0"+val;
		sendProtocol(ledAdres);
//		spOut.write(MessageConvertUtils.toByteArray(
//				getFullProtocol(ledAdres)));
		return Response.ok("成功！");
	}
	
	@ApiOperation(value = "蜂鸣器开关服务")
	@ApiImplicitParam(name = "action",value = "开/关的动作",allowableValues = "00,01")
	@GetMapping("/buzzer/toggle")
	public Response<String> toggle(String action) throws IOException{
		String buzzerAction = "122301"+action;
		sendProtocol(buzzerAction);
//		String protocol = getFullProtocol(buzzerAction);
//		System.out.println("即将写出的控制指令:"+protocol);
//		spOut.write(MessageConvertUtils.toByteArray(protocol));
		return Response.ok("成功！");
	}
	//向串口写出控制指令
	private void sendProtocol(String shortProtocol) throws IOException {
		String protocol = getFullProtocol(shortProtocol);
		System.out.println("即将写出的控制指令:"+protocol);
		spOut.write(MessageConvertUtils.toByteArray(protocol));
	}
	//组装完整协议(完整控制指令)
	private String getFullProtocol(String device) {
		//传递进来的设备指令，如果第三位的字符是2/4代表该指令需要CRC校验。
		if(device.charAt(2)=='2'||device.charAt(2)=='4') {
			device+=CrcModbusCheckUtils.getCRC3(
					MessageConvertUtils.toByteArray(device));
		}
		return prefix+shortAdres+device+suffix;
	}
	
}




